//
//  NewsViewController.m
//  THiOSWeb
//
//  Created by Dexterlirush on 13-8-29.
//  Copyright (c) 2013年 Dexterlirush. All rights reserved.
//

#import "NewsViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Util.h"
#import "NetworkOperation.h"

@interface NewsViewController ()
@property (nonatomic, strong) IBOutlet UIImageView *thumbImageView;
@property (nonatomic, strong) IBOutlet UILabel *titleLabel;
@property (nonatomic, strong) IBOutlet UILabel *publishTimeLabel;
@property (nonatomic, strong) IBOutlet UILabel *locationLabel;
@property (nonatomic, strong) IBOutlet UILabel *organizerLabel;
@property (nonatomic, strong) IBOutlet UILabel *contentLabel;
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong)  NSDictionary *requestParameters;

@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [Util backBarButtonItemWithTarget:self selector:@selector(goBack:)];
    self.navigationItem.rightBarButtonItem = [Util refreshBarButtonItemWithTarget:self selector:@selector(freshDetails:)];
    self.requestParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"operate",@"2",@"content",[NSString stringWithFormat: @"%i", self.newsID ], @"id",nil];
    [self freshDetails:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)freshDetails:(id)sender
{
    
    
    [NetworkOperation sendRequestWithHost:@"http://app.student.tsinghua.edu.cn/android" parameters:self.requestParameters completion:^(BOOL success, id error, id info) {
        if (success) {
            self.titleLabel.text = info[@"title"];
            self.publishTimeLabel.text = info[@"releaseTime"];
            //self.contentLabel.text = info[@"content"];
            self.locationLabel.text = info[@"location"];
            self.organizerLabel.text = info[@"organizer"];
            NSString *img = [NSString stringWithFormat:@"http://student.tsinghua.edu.cn%@", info[@"image"]];
            img = [img stringByReplacingOccurrencesOfString:@"&#x2F;" withString:@"/"];
            [self.thumbImageView setImageWithURL:[NSURL URLWithString:img] placeholderImage:nil];
            NSString *content = info[@"content"];
            content = [content stringByReplacingOccurrencesOfString:@"&#x2F;" withString:@"/"];
            content = [content stringByReplacingOccurrencesOfString:@"/upload" withString:@"http://student.tsinghua.edu.cn/upload"];
            [self.webView loadHTMLString:content baseURL:nil];
        }
        else
        {}
    }];
}

- (void)goBack:(id)sender
{
    [self.webView stopLoading];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
